// packageA/pages/cleanWorkCheck/cleanWorkCheck.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
const util = require('../../../utils/util');
import Base64 from '../../../utils/Base64.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //传参
    paramObj:null,
    //修改信息回显
    editInfo:{},
    //提交按钮禁用
    disabled:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.paramObj){
      let paramObj=JSON.parse(options.paramObj);
      console.log('评价传参');
      console.log(paramObj);

      this.setData({
        paramObj: paramObj
			})
			
			if(paramObj.id){
        //获取修改的数据
        this.getInfo();
      }
    }
  },

  //提交表单##
  confirmForm(e){
    //避免表单重复提交-先禁用按钮
    this.setData({
      disabled:true
    })

    let obj=e.detail.value;
    let td=this.data;
    let editInfo=td.editInfo; 

    if(!editInfo.evaluateScore){
      Toast.fail('请评分');
      return;
    }

    let param={
      className:'cleanKeepingOrder',
      data:{
				id: td.paramObj.id,
				//评分
				evaluateScore: editInfo.evaluateScore,
				//评价内容
				evaluateContent: obj.evaluateContent.trim(),
				//验收时间
				checkTime: util.timeFormat(new Date()),
				//状态变为已完成
				state: 1
      }
    }

    // console.log(param.data);
    // return;

    //避免表单重复提交
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '数据提交中...'
    });

    CommonService.goSave(param).then(res=>{
      this.setData({
        disabled:false
      })
      if (res.statusCode == 200 && res.data.success == true){
        Toast.success('成功');
        setTimeout(() => {
          //返回列表页并刷新数据
          this.backAndRefresh();
        }, 1000);
      }else if(res.statusCode==202){
        Toast.fail('您操作太快了,请勿重复提交');
      }else{
        Toast.fail('操作失败');
      }
    }).catch((error) => {
      Toast.fail('操作失败');
    }) 
  },

  //获取修改数据
  getInfo(){
    let param = {
      isAuth: false,
      className: 'cleanKeepingOrder',
      data: {
        id: this.data.paramObj.id,
				selectFields: ['id','state','project','place','problem','submitter','submissionTime','remark','pic','handler','handlingTime','planCompletionTime','completionTime','handleResult','completionPic','evaluateScore','evaluateContent','checkTime'],
				nbl: Base64.encode(util.timeFormatDay(new Date()))
      }
    }

    CommonService.goFindList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
				let result=res.data.data.result[0];
				
        //创建时间
        if(result.submissionTime){
          result.self_submissionTime=util.timeFormat(result.submissionTime);
				}			

				//开始处理时间
        if(result.handlingTime){
          result.self_handlingTime=util.timeFormat(result.handlingTime);
        }
			
				//预计完成时间
        if(result.planCompletionTime){
          result.self_planCompletionTime=util.timeFormat(result.planCompletionTime);
				}
				
        //实际完成时间
        if(result.completionTime){
          result.self_completionTime=util.timeFormat(result.completionTime);
				}
				
				//验收时间
        if(result.checkTime){
          result.self_checkTime=util.timeFormat(result.checkTime);
        }

        console.log('工单详情');
        console.log(result);
       
        this.setData({
          editInfo: result
        })
      }
    }).catch((error) => {
    })
	},
	
	//评分
	onChangeStar(event) {	
		let evaluateScore='editInfo.evaluateScore';
		this.setData({
			[evaluateScore]: event.detail,
			disabled: false
		})
	},

  //用户输入时清除错误提示
  clearMessage(e) {
    let field = e.target.dataset.message;
    let obj = {};
    obj[field] = "";
    obj['disabled'] = false;
    this.setData(obj);
	},
	
	//返回列表页并刷新数据
  backAndRefresh(){
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];

    prevPage.refreshListAndPage();
    wx.navigateBack();
	}, 
	
	//预览图片
  onPreviewImage(e) {
    let detail = e.target.dataset.detail;
    let src = e.target.dataset.src;

    wx.previewImage({
      current: src,
      urls: detail
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})